package Databasehandler;

import Errorhandler.ErrorLogger;
import java.sql.*;
import OBSXML.DBServerCONF;
import java.io.File;

/**
 * Opretter forbindelse til database
 *
 * @author Børge
 */
public class DBConnector {
    private String dbms, hostName, portNumber, databaseName, username, password;
    private Connection conn;
    DBServerCONF dbConfInfo;
    private String logDir = System.getProperty("user.dir") + File.separator+"log"+File.separator;
    private String logName = "DBConnectorErrors.log";
    public DBConnector ()
    {
        dbConfInfo = new DBServerCONF(File.separator+"app_config"+File.separator, "DBServer.xml");
        this.dbms = dbConfInfo.getDBdbms();
        this.hostName = dbConfInfo.getDBHost();
        this.portNumber = Integer.toString(dbConfInfo.getDBPort());
        this.databaseName = dbConfInfo.getDBName();
        this.username = dbConfInfo.getDBUsername();
        this.password = dbConfInfo.getDBPassword();
    }

    /**
     * Åbner forbindelse til databasen af en type bestemt i xml fil
     *
     * @return Connection
     * @throws SQLException
     */
    private Connection openConn () throws SQLException
    {
  

        //Etablerer forbindelse til database
        if(dbms.equalsIgnoreCase("mysql"))
        {
            //Registrerer driver
            //Documentation: http://download.oracle.com/javase/tutorial/jdbc/basics/connecting.html
            try
            {
                Class.forName("com.mysql.jdbc.Driver");
            }
            catch(ClassNotFoundException ex)
            {
                try {
                ErrorLogger.writeToErrorLog(logDir+logName,"FEJL: "+ex.getMessage()+"\n");
            } catch (Exception ex1){} // fejl hvis der ikke kan skrives til log kan ikke håndteres, SKRIV_TIL_OS_EVENT_LOG
            }
            conn = DriverManager.getConnection("jdbc:"+dbms+"://"+hostName+":"
                    +Integer.parseInt(portNumber)+"/"+databaseName+"?holdResultsOpenOverStatementClose=true", username, password);
        }
        if(dbms.equalsIgnoreCase("mssql"))
        {
            //Documentation: http://msdn.microsoft.com/en-us/library/ms378526(SQL.100).aspx
            //Trouleshoot: http://www.linglom.com/2008/02/23/accessing-sql-server-on-netbeans-using-jdbc-part-iii-troubleshooting/#3
            try
            {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            }
            catch(ClassNotFoundException ex)
            {
                try {
                ErrorLogger.writeToErrorLog(logDir+logName,"FEJL: "+ex.getMessage()+"\n");
            } catch (Exception ex1){} // fejl hvis der ikke kan skrives til log kan ikke håndteres, SKRIV_TIL_OS_EVENT_LOG
            }
            conn = DriverManager.getConnection("jdbc:sqlserver://"+hostName+":"
                    +portNumber+";databaseName="+databaseName+";user="+username+
                    ";password="+password+";");
        }
        this.conn = conn;
        return this.conn;
    }

    /**
     * Åbner et statement
     *
     * @return Statement
     * @throws SQLException
     */
   public Statement createStatement() throws SQLException
   {
       openConn();

       return this.conn.createStatement();
   }

   /**
    * Åbner et preparedstatement
    *
    * @return PreparedStatement
    * @throws SQLException
    */
   public PreparedStatement createPreparedStatement(String query) throws SQLException
   {
       openConn();

       return this.conn.prepareStatement(query);
   }

   /**
    * Lukker forbindelse til databasen
    *
    * @throws SQLException
    */
   public void closeConn() throws SQLException
   {
       this.conn.close();
   }
}
